﻿using System;
using System.Xml;
using Jayden.Dll.Sc2.Enums;

namespace Jayden.Dll.Sc2.Descriptions
{
	public class ResourceActionDescription : ActionDescription
	{
		private ResourceType m_Destination;

		public ResourceActionDescription(EntityDescription entity, XmlNode node)
			: base(entity, entity.Id + "To" + node.Attributes["to"].Value)
		{
			m_Destination = (ResourceType)Enum.Parse(typeof(ResourceType), node.Attributes["to"].Value, false);
		}

		public override string IconId { get { return m_IconId ?? "Gather"; } }
		public override string Name { get { return Entity + " to " + m_Destination.ToString(); } }

		public override bool IsAccessible(GameState state)
		{
			if (m_Destination == ResourceType.Mineral)
				return state.WorkerOnGas > 0;
			if (m_Destination == ResourceType.Vespene)
			{
				GameState.EntityState entityState = state[state.Race.Vespene];
				return state.WorkerOnMineral > 0 && (entityState != null && entityState.Total > 0);
			}
			return base.IsAccessible(state);
		}

		public override bool IsExecutable(GameState state)
		{
			if (m_Destination == ResourceType.Mineral)
				return state.WorkerOnGas > 0;
			if (m_Destination == ResourceType.Vespene)
			{
				GameState.EntityState entityState = state[state.Race.Vespene];
				return state.WorkerOnMineral > 0 && (entityState != null && entityState.Idle > 0);
			}
			return false;
		}

		public override void Execute(GameState state)
		{
			if (m_Destination == ResourceType.Mineral)
			{
				state.WorkerOnMineral++;
				state.WorkerOnGas--;
			}
			if (m_Destination == ResourceType.Vespene)
			{
				state.WorkerOnMineral--;
				state.WorkerOnGas++;
			}
		}
	}
}
